import * as THREE from 'three'
import cameraModule from '../camera'

export default class AlarmSprite {
    constructor(type = '火警', position = { x: -1.8, z: -3 }, color = 0xffffff) {
        const textureLoader = new THREE.TextureLoader();

        const typeObj = {
            '火警': './textures/tag/fire.png',
            '治安': './textures/tag/jingcha.png',
            '电力': './textures/tag/e.png',
        };

        const map = textureLoader.load(typeObj[type]);
        this.material = new THREE.SpriteMaterial({
            map,
            color,
            transparent: true,
            depthTest: false    // 关闭深度检测，否则可能被前面的物体遮挡
        });

        this.mesh = new THREE.Sprite(this.material);
        // 设置位置
        this.mesh.position.set(position.x, 3.5, position.z);
        
        // 封装点击事件
        this.fns = [];
        // 创建射线
        this.raycaster = new THREE.Raycaster();
        this.mouse = new THREE.Vector2();
        // 事件的监听
        window.addEventListener('click', (event) => {
            // 将范围0~1转为-1~1
            // 将鼠标位置归一化为设备坐标。x 和 y 方向的取值范围是 (-1 to +1)
            this.mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
            this.mouse.y = -((event.clientY / window.innerHeight) * 2 - 1);
        
            this.raycaster.setFromCamera(this.mouse, cameraModule.activeCamera);

            event.mesh = this.mesh;
            event.alarm = this;

            const intersects = this.raycaster.intersectObject(this.mesh);
            if(intersects.length > 0) {
                this.fns.forEach(fn => {
                    fn(event)
                })
            }
        })
    }

    onClick(fn) {
        this.fns.push(fn)
    }

    remove() {
        this.mesh.remove()
        this.mesh.removeFromParent()
        this.mesh.geometry.dispose()
        this.mesh.material.dispose()
    }
}